package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.zzyl.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author sjqn
 * @date 2023/11/19
 */
@Slf4j
@Service
public class AlertDataServiceImpl implements AlertDataService {


    @Autowired
    private AlertDataMapper alertDataMapper;


    @Override
    public PageResponse<AlertDataVo> adminPageQuery(AlertDataPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        // 日期处理
        dto.setQueryStartTime(dto.getStartTime()==null?null:LocalDateTimeUtil.of(dto.getStartTime()));
        dto.setQueryEndTime(dto.getEndTime()==null?null:LocalDateTimeUtil.of(dto.getEndTime()));
        // 查询
        Page<AlertData> pageInfo = alertDataMapper.selectPage(dto);
        return PageResponse.of(pageInfo, AlertDataVo.class);
    }

    @Override
    public void handleAlertData(Long id, AlertDataHandleDto alertDataHandleDto) {
        AlertData updateData = BeanUtil.toBean(alertDataHandleDto, AlertData.class);
        // 登录用户
        String userJson = UserThreadLocal.getSubject();
        UserVo loginUser = JSONUtil.toBean(userJson, UserVo.class);
        updateData.setProcessorId(loginUser.getId());
        updateData.setProcessorName(loginUser.getRealName());
        updateData.setStatus(1);//处理的状态，1：已处理
        updateData.setId(id);
        alertDataMapper.updateByPrimaryKeySelective(updateData);
    }
}
